
<style>
    .spanColor{
        color: red;
    }
    .layui-form-label {
        width: 150px;
    }
    .layui-input{
        width: 200px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">物料编号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号" autocomplete="off"/>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" id="inventoryInoutSearch" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="inventoryKnifeRepairBtnExport" class="layui-btn icon-btn color-green">导出
                            </button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="storageInventoryKnifeRepairTable" lay-filter="storageInventoryKnifeRepairTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="storageInventoryKnifeRepairTableBar">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">设置</a> -->
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;


        // 渲染表格
        var insTb = table.render({
            elem: '#storageInventoryKnifeRepairTable',
            url: config.base + 'enterprisecab/cabinet/admin/storageInventoryKnifeRepair/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            limit: 5,
            limits:[5,10,20,30,40,50,60,70,80],
            cellMinWidth: 100,
            cols: [[
                // {type: 'checkbox'}
                 {type: 'numbers', title: '序号', fixed: 'left'}
                ,{field: 'skuPic', title: '图片', fixed: 'left', width: 110,templet:function (d) {
                        if(d.skuPic){
                            return '<img src="'+ d.skuPic +'" style="height: 20px;"/>';
                        }
                        return '';
                    }}
                ,{field: 'skuName', title: '物料名称', fixed: 'left', width: 160}
                ,{field: 'skuCode', title: '物料编号', fixed: 'left', width: 160}
                ,{field: 'skuSpecOld', title: '规格码', fixed: 'left', width: 160}
                ,{field: 'productBrandName', title: '品牌'}
                ,{field: 'cabCode', title: '柜子编号', width: 130}
                ,{field: 'spaceCode', title: '货位编号'}
                ,{field: 'quantity', title: '数量',templet:function (d) {
                        return d.quantity + d.basicsUnitName;
                    }}
                ,{field: 'labelCode', title: '刀具条码', width: 130}
                ,{field: 'remainingTime', title: '刀具寿命',templet:function (d) {
                        if(d.lifeUnitDesc==''){
                            return '不计寿命';
                        }
                        return d.remainingTime+'/'+d.settingTime;
                    }}
                ,{field: 'lifeUnitDesc', title: '寿命单位', templet:d=> d.lifeUnitDesc ? d.lifeUnitDesc : '无'}
                ,{field: 'createTime', sort: true, title: '创建时间', width: 160}
                // ,{align: 'center', toolbar: '#storageInventoryKnifeRepairTableBar', title: '操作', width: 200}
            ]],
        });

        // 工具条点击事件
        table.on('tool(storageInventoryKnifeRepairTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showUpdataModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 工具条点击事件
        table.on('tool(addTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'add') { //添加智能柜存放物资类型
                layer.confirm('确定要添加到智能柜存放物资类型？', {title: "操作提示", btn: ['确定', '取消']}
                , function(){ // 确定操作
                        addSearch(data);
                });
            }
        });

        //监听排序
        table.on('sort(storageInventoryKnifeRepairTable)', function(obj){
            table.reload('storageInventoryKnifeRepairTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#storeProductBtnAdd').click(function () {
            showEditModel();
        });

        function addSearch(data){
            admin.req('enterprisecab/cabProduct/admin/storeProductType/upProductType',data, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    form.render('select');
                    table.reload('addTable');
                    table.reload('storageInventoryKnifeRepairTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'PUT');
        }

        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         */
        function loadSelectHtml(data, tips, htmltarget, name) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                html += `<option value='${data[i].id}'>${data[i][name]}</option>`;
            }
            $("[name='" + htmltarget + "']").html(html);
            form.render('select');
        }


        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/dic/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }
        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });
        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 导出excel
        $('#inventoryKnifeRepairBtnExport').click(function () {
            location.href = config.base + 'enterprisecab/cabinet/admin/storageInventoryKnifeRepair/export';
        });

    });
</script>